/**
 * Created by dukes on 2016/1/5.
 */
class GoldenFingerPanel extends eui.Component{
    public constructor(){
        super();
        this.skinName = "pet.GoldenFingerSkin";
        this.addEventListener(egret.Event.ADDED_TO_STAGE,this.onAdded, this );
        this.addEventListener(egret.Event.REMOVED_FROM_STAGE, this.onRemoved, this);
    }
    private curConfig:BathConfig;
    private info;
    public updateDisplay(info){
        this.info = info;
        this.curConfig = ActivityMgr.getInstance().getCurBathConfig(info.washTimes);
        this.img_lv.text = Model.me.vipLevel+"";// RES.getRes("preload_json.num_4_"+Model.me.vipLevel);
        var restTime:number = Model.userExt.maxWashTimes - info.washTimes;
        if( restTime > 0){
            this.lab_times.text =  restTime + "";
        }else{
            this.lab_times.text =  "0";
        }

        if( PetMgr.getInstance().curPet.type == Model.GOOD_TYPE_CAT){
            this.img_type.source = RES.getRes("preload_json.img_type_cat");
        }else{
            this.img_type.source = RES.getRes("preload_json.img_type_dog");
        }
        this.lab_cnt.text = info.sumHair+"";
        this.lab_diamon.text = "x"+(this.curConfig != null ? this.curConfig.diamond: 0);
    }

    public show(){
        DisplayController.instance().addToAlert(GameConst.GrayMask);
        DisplayController.instance().addToAlert(this);
    }

    private onClose(evt:egret.TouchEvent=null){
        if( evt )
            SoundMgr.getInstance().playEffect(SoundMgr.BTN);
        DisplayController.instance().removeFromAlert(GameConst.GrayMask);
        DisplayController.instance().removeFromAlert(this);
    }

    private onGet(evt:egret.TouchEvent){
        SoundMgr.getInstance().playEffect(SoundMgr.BTN);
        if( Model.userExt.maxWashTimes <= this.info.washTimes){
            ShowHint("每日6点重置该功能")
        }else if( Model.me.diamond >= this.curConfig.diamond){
            window["__wash"](PetMgr.getInstance().curPet.petId);
        }else{
            this.onClose();
            //SceneMgr.getInstance().switchScene(SceneMgr.SCENE_RECHARGE);
            BuyDiamondPanel.getInstance().show();
        }
    }
    private onAdded(evt:egret.Event){
        this.img_close.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onClose, this);
        this.img_get.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onGet, this);
        window["__washInfo"](PetMgr.getInstance().curPet.petId);
    }

    public onWashBack(hehe){
        if( hehe.rsp.hair.type == Model.GOOD_TYPE_DOG){
            ShowHint("获得"+ hehe.rsp.wash.sumHair+"狗毛");
        }else{
            ShowHint("获得"+hehe.rsp.wash.sumHair+"猫毛");
        }
        hehe.rsp.wash.sumHair = hehe.rsp.nextSumHair;
        this.updateDisplay(hehe.rsp.wash);
    }

    private onRemoved(evt:egret.Event){
        this.img_close.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onClose, this);
        this.img_get.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onGet, this);
    }
    public img_lv:eui.BitmapLabel;
    public img_get:eui.Image;
    public img_close:eui.Image;
    public lab_times:eui.Label;
    public lab_cnt:eui.Label;
    public img_type:eui.Image;
    public lab_diamon:eui.Label;

    public partAdded(name:string, instance:any) {
        super.partAdded(name, instance);
        if (name == "img_lv") {
            this.img_lv = instance;
        } else if (name == "img_get") {
            this.img_get = instance;
        } else if (name == "img_close") {
            this.img_close = instance;
        } else if (name == "lab_times") {
            this.lab_times = instance;
        } else if (name == "img_type") {
            this.img_type = instance;
        }else if (name == "lab_diamon") {
            this.lab_diamon = instance;
        }else if( name == "lab_cnt"){
            this.lab_cnt = instance;
        }
    }
    public measure() {
        super.measure();
        this.x = GameConst.StageW - this.measuredWidth >> 1;
        this.y = GameConst.StageH - this.measuredHeight >> 1;
    }
    private static _instance:GoldenFingerPanel;
    public static getInstance():GoldenFingerPanel{
        if( GoldenFingerPanel._instance == null)
            GoldenFingerPanel._instance = new GoldenFingerPanel();
        return GoldenFingerPanel._instance;
    }
}